import { BufferGeometry, CatmullRomCurve3, Line, LineBasicMaterial, Mesh, Path, SplineCurve } from "three";
import { App } from "../../../../Engine";
import Const from "../../../../application/constants/Const";

export default class FlowLine extends Mesh {
    private app: App;
    private points;
    public positions = [];
    public pointColor;
    public lineColor;
    private firstPoint;
    private line;

    constructor(cfg: any) {
        super();

        this.positions = cfg.positions;
        this.positions.forEach(element => {
            element.toEPSGWeb();
        });
        this.pointColor = cfg.pointColor ? cfg.pointColor : 0x006666;//默认青色
        this.lineColor = cfg.lineColor ? cfg.lineColor : 0xffff00;//默认黄色
        this.firstPoint = this.positions[0].clone();
        this.makeLine();
    }

    /**
    * 生成线对象
    */
    private makeLine() {
        var geometry = new BufferGeometry(); //创建一个缓冲类型几何体

        // 三维样条曲线
        var curve = new CatmullRomCurve3(this.positions);
        curve.curveType = 'catmullrom';
        curve.tension = 0;

        //曲线上等间距返回多个顶点坐标
        this.points = curve.getSpacedPoints(1000); //分段数100，返回101个顶点
        this.points.forEach(element => {
            element.sub(this.firstPoint);
        });

        // setFromPoints方法从points中提取数据赋值给attributes.position
        geometry.setFromPoints(this.points);
        var material = new LineBasicMaterial({
            color: 0x006666, //轨迹颜色
        });
        //线条模型对象
        this.line = new Line(geometry, material);
        this.line.position.copy(this.firstPoint);
        this.add(this.line);
    }

}